package com.turing.post.organization.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.MerchantPaidFee;
import com.turing.post.organization.util.MerchantPaidFeeBusiness;
import com.turing.post.organization.util.MerchantPaidFeeDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 持卡人商户消费手续费（MerchantPaidFee）
 * 
 * @author zxp
 * @Jan 3, 2013
 */
@Stateless(mappedName = "organization.impl.MerchantPaidFeeBusinessImpl")
@Remote
@Local
public class MerchantPaidFeeBusinessImpl implements MerchantPaidFeeBusiness {

	@EJB(mappedName = "organization.impl.MerchantPaidFeeDaoImpl")
	private MerchantPaidFeeDao merchantPaidFeeDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	@Override
	public void insertMerchantPaidFeeInfo(PCPSWebInput webInput,
			MerchantPaidFee merchantPaidFee) throws PCPSApplicationException {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| webInput.getOperId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"登录操作员归属机构号为空!");
		}
		if (PCPSUtil.isEmpty(merchantPaidFee.getMerchantNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"商户号为空！");
		}
		if (PCPSUtil.isEmpty(merchantPaidFee.getBusinessType())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"业务类型为空！");
		}
		if (PCPSUtil.isEmpty(merchantPaidFee.getBusinessName())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"业务名称为空！");
		}
		if (merchantPaidFee.getFeeRatio() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"费率为空！");
		}

		MerchantPaidFee mpf = merchantPaidFeeDaoImpl.getMerchantPaidFeeByKey(
				merchantPaidFee.getMerchantNo(), merchantPaidFee
						.getBusinessType());
		if (mpf != null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"该商户业务类型已经存在！");
		}
		merchantPaidFeeDaoImpl.insert(merchantPaidFee);
		// 插入数据库操作日志
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "添加持卡人商户消费手续费", "MerchantNo:"
						+ merchantPaidFee.getMerchantNo() + "BusinessName:"
						+ merchantPaidFee.getBusinessName());
	}

	@Override
	public void updateMerchantPaidFeeInfo(PCPSWebInput webInput,
			MerchantPaidFee mpf) throws PCPSApplicationException {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| webInput.getOperId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"登录操作员归属机构号为空!");
		}
		if (PCPSUtil.isEmpty(mpf.getMerchantNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"商户号为空！");
		}
		if (PCPSUtil.isEmpty(mpf.getBusinessType())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"业务类型为空！");
		}
		MerchantPaidFee uddateMpf = merchantPaidFeeDaoImpl
				.getMerchantPaidFeeByKey(mpf.getMerchantNo(), mpf
						.getBusinessType());
		if (uddateMpf == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"待修改持卡人商户消费手续费为空！");
		}

		if (PCPSUtil.isEmpty(mpf.getBusinessName())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"业务名称为空！");
		}
		if (mpf.getFeeRatio() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"费率为空！");
		}
		uddateMpf.setBusinessDesc(mpf.getBusinessDesc());
		uddateMpf.setBusinessName(mpf.getBusinessName());
		uddateMpf.setFeeRatio(mpf.getFeeRatio());
		merchantPaidFeeDaoImpl.update(uddateMpf);
		// 插入数据库操作日志
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Edit, "修改持卡人商户消费手续费", "MerchantNo:"
						+ uddateMpf.getMerchantNo() + "BusinessName:"
						+ uddateMpf.getBusinessName());
	}

	@Override
	public void deleteMerchantPaidFeeInfo(PCPSWebInput webInput,
			MerchantPaidFee mpf) throws PCPSApplicationException {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| webInput.getOperId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"登录操作员归属机构号为空!");
		}
		if (PCPSUtil.isEmpty(mpf.getMerchantNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"商户号为空！");
		}
		if (PCPSUtil.isEmpty(mpf.getBusinessType())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"业务类型为空！");
		}
		MerchantPaidFee deleteMpf = merchantPaidFeeDaoImpl
				.getMerchantPaidFeeByKey(mpf.getMerchantNo(), mpf
						.getBusinessType());
		if (deleteMpf == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"待删除持卡人商户消费手续费为空！");
		}
		// 插入数据库操作日志
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Delete, "删除持卡人商户消费手续费", "MerchantNo:"
						+ deleteMpf.getMerchantNo() + "BusinessName:"
						+ deleteMpf.getBusinessName());
		merchantPaidFeeDaoImpl.remove(deleteMpf);
	}
}